/**   
 * Model Created on 2022年9月1日
 *   
 * Copyright (c) 2018 by Kunrong.HOo.
 */ 
package com.seaside.park.api;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seaside.common.bean.Result;
import com.seaside.park.bean.Complain;
import com.seaside.park.service.ComplainService;
import com.seaside.parkCommon.bean.User;
import com.seaside.parkCommon.mapper.UserMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @author <a href="mailto:hoocoral@hotmail.com">Kunrong.HOo</a>
 * @version 1.0
 */
@Api(tags="投诉")
@RestController
@RequestMapping("/api/complain")
public class ComplainAPI {
	
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ComplainService complainService;
	
    @ApiOperation(value = "投诉列表", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="curpage",dataTypeClass=Integer.class,required=true,value="当前页")
    })
    @PostMapping("/list")
    public Result<IPage<Complain>> list(@ApiIgnore Authentication auth, int curpage) {
    	
        Page<Complain> page = new Page<>(curpage, 15, false);
        
        User user = userMapper.selectById(auth.getName());
        if (user == null) {
        	return Result.Error("未找到该用户！");
        }
        
        QueryWrapper<Complain> queryWrapper = new QueryWrapper<>();
        if ("manager".equals(user.getUserType())) {
        	// 管理员不需要权限
        } else if ("user".equals(user.getUserType())) {
        	queryWrapper.eq("create_user_id", user.getUserGuid());
        } else {
        	return Result.Error("无权查看！");
        }
        
        queryWrapper.orderByDesc("create_time");
        
        Result<IPage<Complain>> iPageRet = complainService.getComplainListPage(page, queryWrapper);
        if (iPageRet.isSucess()) {
        	IPage<Complain> complainRet = iPageRet.getData();
        	return Result.OK("", complainRet.getRecords());
        }
        return Result.Error(iPageRet.getMsg());
    }
    
    @ApiOperation(value = "告警分页列表", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="curpage",dataTypeClass=Integer.class,required=true,value="当前页")
    })
    @PostMapping("/page")
    public Result<IPage<Complain>> getComplainListPage(@ApiIgnore Authentication auth, int curpage) {
    	
        Page<Complain> page = new Page<>(curpage, 15, false);
        
        User user = userMapper.selectById(auth.getName());
        if (user == null) {
        	return Result.Error("未找到该用户！");
        }
        
        QueryWrapper<Complain> queryWrapper = new QueryWrapper<>();
        if ("manager".equals(user.getUserType())) {
        	// 管理员不需要权限
        } else if ("user".equals(user.getUserType())) {
        	queryWrapper.eq("create_user_id", user.getUserGuid());
        } else {
        	return Result.Error("无权查看！");
        }
        queryWrapper.orderByDesc("create_time");
        
        Result<IPage<Complain>> iPageRet = complainService.getComplainListPage(page, queryWrapper);
        
        return iPageRet;
    }
    
    @ApiOperation(value = "投诉上报", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token")
    })
    @PostMapping("/save")
    public Result<Complain> saveComplain(@ApiIgnore Authentication auth, 
    		@RequestBody Complain complain) {
    	
    	if (complain == null) {
    		return Result.Error("参数错误！");
    	}

        User user = userMapper.selectById(auth.getName());
        if (user == null) {
        	return Result.Error("用户错误！");
        }
        
        complain.setCreateTime(new Date());
        complain.setCreateUserName(user.getUserName());
        complain.setCreateUserId(user.getUserGuid());
        complain.setStatus("0");
        
        return complainService.saveComplain(complain);
    }
    
    @ApiOperation(value = "投诉处理", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token")
    })
    @PostMapping("/handle")
    public Result<Boolean> doComplainHandle(@ApiIgnore Authentication auth, 
    		@RequestBody Complain complain) {
    	
    	if (complain == null) {
    		return Result.Error("参数错误！");
    	}

    	if (!"1".equals(complain.getStatus()) && !"2".equals(complain.getStatus())) {
    		return Result.Error("状态参数错误！");
    	}
    	
        User user = userMapper.selectById(auth.getName());
        if (user == null) {
        	return Result.Error("用户错误！");
        }
    	
    	Result<Complain> complainRet = complainService.getById(complain.getId());
    	if (!complainRet.isSucess()) {
            return Result.Error(complainRet.getMsg());
    	}
    	

    	Complain dbData = complainRet.getData();
    	if ("1".equals(dbData.getStatus())) {
    		return Result.Error("该投诉已处理！");
    	} else {
    		dbData.setStatus(complain.getStatus());
    		dbData.setFeedback(complain.getFeedback());
    		dbData.setDealerId(user.getUserGuid());
    		dbData.setDealerName(user.getUserName());
    		dbData.setDealTime(new Date());
    	}
		return complainService.updateComplain(dbData);
    }
    
    @ApiOperation(value = "查询投诉详情", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="id",dataTypeClass=Integer.class,required=true,value="告警ID")
    })
    @GetMapping("/detail/{id}")
    public Result<Complain> getComplainDetail(@ApiIgnore Authentication auth, 
    		@PathVariable("id") Integer id) {
    	
    	if (id == null) {
    		return Result.Error("参数错误！");
    	}
    	return complainService.getById(id);
    }

}